import java.util.Random;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class BubbleSprite extends Sprite {
	
	private static final int BUBBLE_COLOR = 0x739EFF;
	
	public int radius;
	public int diameter;
	private int wCanvas;
	private int hCanvas;
	
	public BubbleSprite(int r, int wCanvas, int hCanvas) {
		super(BubbleSprite.createImage(r + r));
		this.radius = r;
		this.diameter = r + r;
		this.wCanvas = wCanvas;
		this.hCanvas = hCanvas;
		
		setPosition(getRandomPosX(), hCanvas);
	}
	
	public int getRandomPosX() {
		return new Random().nextInt(wCanvas - diameter);
	}
	
	public void moveUp(int dy) {
		move(0, -dy);
		if (getY() + diameter < 0) {
			setPosition(getX(), hCanvas);
		}
	}
	
	public static Image createImage(int s) {
		Image img = Image.createImage(s, s);
		Graphics g = img.getGraphics();
		g.setColor(BUBBLE_COLOR);
		g.fillArc(0, 0, s, s, 0, 360);
		
		int[] rgb = new int[s * s];
		img.getRGB(rgb, 0, s, 0, 0, s, s);
		
		int k = 0;
		for (int i = 0; i < s; i++)
			for (int j = 0; j < s; j++) {
				if ((rgb[k] & 0xFFFFFF) == 0xFFFFFF) {
					rgb[k] = 0x00FFFFFF;
				}
				k++;
			}
		
		return Image.createRGBImage(rgb, s, s, true);
	}

}
